package com.sany.taskmanager.ui.widget;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.sany.taskmanager.R;
import com.sany.taskmanager.bean.TaskInfoBean;
import com.sany.taskmanager.databinding.DialogModifyTaskInfoBinding;
import com.sany.taskmanager.util.EditUtil;
import com.sany.taskmanager.util.ToastUtil;

/**
 * 修改下发的任务信息
 */
public class ModifyTaskInfoDialog extends CommonDialog<DialogModifyTaskInfoBinding> implements View.OnClickListener {
    private final Context mContext;
    private OnModifyTaskInfoListener mOnModifyTaskInfoListener;
    private int mColorType;
    private String mColorCode;
    private String mColorValue;
    private int mHeadColorType;
    private String mHeadColorCode;
    private String mHeadColorValue;
    private String mMaterialNum;
    private int mBoomType;
    private final boolean mExistCount;
    private final boolean mExistMaterialNum;
    private int mCount = 1;

    @Override
    protected void setRect() {
        mWidth = 750;
        mHeight = 480;
        mTitleMarginTop = 36;
        mTitleMarginStart = 66;
    }

    public void setOnModifyTaskInfoListener(OnModifyTaskInfoListener onModifyTaskInfoListener) {
        mOnModifyTaskInfoListener = onModifyTaskInfoListener;
    }

    public ModifyTaskInfoDialog(@NonNull Context context, TaskInfoBean taskInfoBean) {
        this(context, taskInfoBean, false);
    }

    public ModifyTaskInfoDialog(@NonNull Context context, TaskInfoBean taskInfoBean, boolean existCount) {
        this(context, taskInfoBean, existCount, true);
    }

    public ModifyTaskInfoDialog(@NonNull Context context, TaskInfoBean taskInfoBean, boolean existCount, boolean existMaterialNum) {
        super(context, context.getString(R.string.tip_modify_task_info));
        mContext = context;
        mExistCount = existCount;
        mExistMaterialNum = existMaterialNum;
        updateData(taskInfoBean);
    }

    public void setTaskInfo(TaskInfoBean taskInfoBean) {
        updateData(taskInfoBean);
        updateUI();
    }

    private void updateData(TaskInfoBean taskInfoBean) {
        mCount = taskInfoBean.getCount();
        mColorType = taskInfoBean.getColorType();
        mHeadColorType = taskInfoBean.getHeadColorType();
        mMaterialNum = taskInfoBean.getMaterialNum();
        mBoomType = taskInfoBean.getBoomType();
        mColorCode = taskInfoBean.getColorCode();
        mColorValue = taskInfoBean.getColorValue();
        mHeadColorCode = taskInfoBean.getHeadColorCode();
        mHeadColorValue = taskInfoBean.getHeadColorValue();
    }

    public void setMaterialNum(String materialNum) {
        mMaterialNum = materialNum;
        vBinding.etMaterialNum.setText(mMaterialNum);
        vBinding.etMaterialNum.setSelection(mMaterialNum.length());
    }

    @Override
    protected void initData() {
        vBinding.llNewBoom.setOnClickListener(v -> {
            mBoomType = Math.abs(mBoomType - 1);
            vBinding.ivNewBoom.setVisibility(mBoomType == 1 ? View.VISIBLE : View.GONE);
        });
        vBinding.ivScan.setOnClickListener(v -> {
            if (mOnModifyTaskInfoListener != null) {
                mOnModifyTaskInfoListener.goScan();
            }
        });
        vBinding.etMaterialNum.setTransformationMethod(EditUtil.replacementTransformationMethod);
        vBinding.rlBoomColor.setOnClickListener(this);
        vBinding.rlBoomHeadColor.setOnClickListener(this);
        updateUI();
    }

    private void updateUI() {
        vBinding.ivNewBoom.setVisibility(mBoomType == 1 ? View.VISIBLE : View.GONE);
        if (mExistCount) {
            vBinding.tvCount.setVisibility(View.VISIBLE);
            vBinding.etCount.setVisibility(View.VISIBLE);
            vBinding.etCount.setText(String.valueOf(mCount));
            vBinding.etCount.setSelection(String.valueOf(mCount).length());
        } else {
            vBinding.tvCount.setVisibility(View.GONE);
            vBinding.etCount.setVisibility(View.GONE);
        }
        if (mExistMaterialNum) {
            vBinding.tvMaterialNum.setVisibility(View.VISIBLE);
            vBinding.etMaterialNum.setVisibility(View.VISIBLE);
            vBinding.ivScan.setVisibility(View.VISIBLE);
            vBinding.etMaterialNum.setText(mMaterialNum);
            vBinding.etMaterialNum.setSelection(mMaterialNum.length());
        } else {
            vBinding.tvMaterialNum.setVisibility(View.GONE);
            vBinding.etMaterialNum.setVisibility(View.GONE);
            vBinding.ivScan.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(mColorCode)) {
            vBinding.tvColor.setVisibility(View.GONE);
            vBinding.viewColor.setVisibility(View.GONE);
        } else {
            vBinding.tvColor.setVisibility(View.VISIBLE);
            vBinding.viewColor.setVisibility(View.VISIBLE);
            vBinding.tvColor.setText(mColorCode);
            vBinding.viewColor.setBgColor(Color.parseColor(mColorValue));
        }
        if (TextUtils.isEmpty(mHeadColorCode)) {
            vBinding.tvHeadColor.setVisibility(View.GONE);
            vBinding.viewHeadColor.setVisibility(View.GONE);
        } else {
            vBinding.tvHeadColor.setVisibility(View.VISIBLE);
            vBinding.viewHeadColor.setVisibility(View.VISIBLE);
            vBinding.tvHeadColor.setText(mHeadColorCode);
            vBinding.viewHeadColor.setBgColor(Color.parseColor(mHeadColorValue));
        }
    }

    @Override
    protected boolean confirm() {
        if (checkTaskInfo()) {
            if (mOnModifyTaskInfoListener != null) {
                mOnModifyTaskInfoListener.modifyTaskInfo(mColorType, mColorCode, mColorValue, mHeadColorType, mHeadColorCode, mHeadColorValue, vBinding.etMaterialNum.getText().toString().toUpperCase().replace(" ", ""), mBoomType, mCount);
            }
        }
        return true;
    }

    private boolean checkTaskInfo() {
        if (mExistMaterialNum) {
            mMaterialNum = vBinding.etMaterialNum.getText().toString().replaceAll(" ", "");
            if (mMaterialNum.length() == 0) {
                ToastUtil.showToast(mContext, mContext.getString(R.string.please_input_material_num));
                return false;
            }
        }
        if (mExistCount) {
            return checkCount();
        }
        return true;
    }

    private boolean checkCount() {
        String count = vBinding.etCount.getText().toString().replaceAll(" ", "");
        if (count.length() == 0) {
            ToastUtil.showToast(mContext, mContext.getString(R.string.please_input_boom_count));
            return false;
        }
        try {
            mCount = Integer.parseInt(count);
            if (mCount <= 0) {
                ToastUtil.showToast(mContext, mContext.getString(R.string.please_input_correct_boom_count));
                return false;
            }
        } catch (Exception e) {
            ToastUtil.showToast(mContext, mContext.getString(R.string.please_input_correct_boom_count));
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.rl_boom_color) {
            showColorDialog();
        } else if (id == R.id.rl_boom_head_color) {
            showHeadColorDialog();
        }
    }

    private ColorDialog colorDialog;

    /**
     * 大臂颜色选择弹框
     */
    private void showColorDialog() {
        if (colorDialog == null) {
            colorDialog = new ColorDialog(mContext);
            colorDialog.setOnSelectedColorListener(colorBean -> {
                mColorType = colorBean.getColorType();
                mColorCode = colorBean.getColorCode();
                mColorValue = colorBean.getColorValue();
                vBinding.tvColor.setVisibility(View.VISIBLE);
                vBinding.viewColor.setVisibility(View.VISIBLE);
                vBinding.viewColor.setBgColor(Color.parseColor(colorBean.getColorValue()));
                vBinding.tvColor.setText(colorBean.getColorCode());
            });
        }
        if (!colorDialog.isShowing()) {
            colorDialog.show();
        }
    }

    private ColorDialog headColorDialog;

    /**
     * 大臂颜色选择弹框
     */
    private void showHeadColorDialog() {
        if (headColorDialog == null) {
            headColorDialog = new ColorDialog(mContext);
            headColorDialog.setOnSelectedColorListener(colorBean -> {
                mHeadColorType = colorBean.getColorType();
                mHeadColorCode = colorBean.getColorCode();
                mHeadColorValue = colorBean.getColorValue();
                vBinding.tvHeadColor.setVisibility(View.VISIBLE);
                vBinding.viewHeadColor.setVisibility(View.VISIBLE);
                vBinding.viewHeadColor.setBgColor(Color.parseColor(colorBean.getColorValue()));
                vBinding.tvHeadColor.setText(colorBean.getColorCode());
            });
        }
        if (!headColorDialog.isShowing()) {
            headColorDialog.show();
        }
    }

    public interface OnModifyTaskInfoListener {
        void modifyTaskInfo(int colorType, String colorCode, String colorValue, int headColorType, String headColorCode, String headColorValue, String materialNum, int boomType, int count);

        void goScan();
    }

}
